import { apiSelectCity, apiSelectCate, apiSelectUser, apiAssetAllIndustry } from '@/api/assetsPublic'
import { apiNoticeSelect } from '@/api/biddingNotice'
import { apiLabelBaseSelect } from '@/api/dataStatistics'

const state = () => {
  return {
    cityList: [], // 地区列表
    cateList: [], // 分类列表
    noticeCateList: [], // 招标拍卖分类列表
    userList: [], // 用户列表
    industryList: [], // 行业列表.
    evaluationList: [], // 估值列表
    labelBaseInfo: null, // 用户画像基本信息
  }
}

const mutations = {
  SET_CITY_LIST: (state, list) => {
    state.cityList = list
  },
  SET_CATE_LIST: (state, list) => {
    state.cateList = list
  },
  SET_NOTICE_CATE_LIST: (state, list) => {
    state.noticeCateList = list
  },
  SET_USER_LIST: (state, list) => {
    state.userList = list
  },
  SET_INDUSTRY_LIST: (state, list) => {
    state.industryList = list
  },
  SET_EVALUATION_LIST: (state, list) => {
    state.evaluationList = list
  },
  SET_LABEL_BASE_INFO: (state, info) => {
    state.labelBaseInfo = info
  },
}

const actions = {
  getCityList({ commit, state }) {
    return new Promise((resolve, reject) => {
      if (state.cityList.length > 0) {
        resolve(state.cityList)
      } else {
        apiSelectCity()
          .then((res) => {
            commit('SET_CITY_LIST', res.data)
            resolve(res.data)
          })
          .catch((error) => {
            reject(error)
          })
      }
    })
  },
  getCateList({ commit, state }) {
    return new Promise((resolve, reject) => {
      if (state.cateList.length > 0) {
        resolve(state.cateList)
      } else {
        apiSelectCate()
          .then((res) => {
            res.data.forEach((item) => {
              if (item.children.length === 0) {
                delete item.children
              } else {
                item.children.forEach((item2) => {
                  if (item2.children.length === 0) {
                    delete item2.children
                  }
                })
              }
            })
            commit('SET_CATE_LIST', res.data)
            resolve(res.data)
          })
          .catch((error) => {
            reject(error)
          })
      }
    })
  },
  getNoticeCateList({ commit, state }) {
    return new Promise((resolve, reject) => {
      if (state.noticeCateList.length > 0) {
        resolve(state.noticeCateList)
      } else {
        apiSelectCate({
          flag: 2,
        })
          .then((res) => {
            res.data.forEach((item) => {
              if (item.children.length === 0) {
                delete item.children
              } else {
                item.children.forEach((item2) => {
                  if (item2.children.length === 0) {
                    delete item2.children
                  }
                })
              }
            })
            commit('SET_NOTICE_CATE_LIST', res.data)
            resolve(res.data)
          })
          .catch((error) => {
            reject(error)
          })
      }
    })
  },
  getUserList({ commit, state }) {
    return new Promise((resolve, reject) => {
      if (state.userList.length > 0) {
        resolve(state.userList)
      } else {
        apiSelectUser()
          .then((res) => {
            res.data = res.data.map((item) => {
              return {
                value: item.id,
                label: item.name,
              }
            })
            commit('SET_USER_LIST', res.data)
            resolve(res.data)
          })
          .catch((error) => {
            reject(error)
          })
      }
    })
  },
  getIndustryList({ commit, state }) {
    return new Promise((resolve, reject) => {
      if (state.industryList.length > 0) {
        resolve(state.industryList)
      } else {
        apiAssetAllIndustry()
          .then((res) => {
            commit('SET_INDUSTRY_LIST', res.data)
            resolve(res.data)
          })
          .catch((error) => {
            reject(error)
          })
      }
    })
  },
  getEvaluationList({ commit, state }) {
    return new Promise((resolve, reject) => {
      if (state.evaluationList.length > 0) {
        resolve(state.evaluationList)
      } else {
        apiNoticeSelect()
          .then((res) => {
            res.data = res.evaluation.map((item) => {
              return {
                value: item.value,
                label: item.key,
              }
            })
            commit('SET_INDUSTRY_LIST', res.data)
            resolve(res.data)
          })
          .catch((error) => {
            reject(error)
          })
      }
    })
  },
  getLabelBaseInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      if (state.labelBaseInfo) {
        resolve(state.labelBaseInfo)
      } else {
        apiLabelBaseSelect()
          .then((res) => {
            const data = { dataList: res.data, condition: res.condition }
            commit('SET_LABEL_BASE_INFO', data)
            resolve(data)
          })
          .catch((error) => {
            reject(error)
          })
      }
    })
  },
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
}
